package com.bsoft.ecommerce.controller;

import com.alibaba.fastjson.JSON;
import com.bsoft.ecommerce.annotation.IgnoreResponseAdvice;
import com.bsoft.ecommerce.service.IJWTService;
import com.bsoft.ecommerce.vo.JwtToken;
import com.bsoft.ecommerce.vo.UsernameAndPassword;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @program: e-commerce-springclound
 * @description:
 * @author: hdh
 * @create: 2022:06:21 13:44
 */
//对外 暴露的授权服务接
@Slf4j
@RestController
@RequestMapping("/authority")
public class AuthorityController {

    private  final IJWTService   ijwtService;
    public  AuthorityController (IJWTService ijwtService){
        this.ijwtService= ijwtService;
    }
    //从授权中心 获取token  (登录功能)
    @IgnoreResponseAdvice
    @PostMapping("/token")
    public JwtToken token(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception {
    log.info("请求对象"+ JSON.toJSONString(usernameAndPassword));
    log.info("-----------------------------------------------------------------------------------------------------------");
     return  new JwtToken(ijwtService.generateToken(usernameAndPassword.getUsername(),usernameAndPassword.getPassword()));
    }
    //注册用户 并返回当前用户的token  即通过授权中心创建用户
    @IgnoreResponseAdvice
    @PostMapping("/register")
    public  JwtToken register(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception {
        log.info("--------------------------hahahahhahahahahahahahhahahahahahahhahahahahahaahahah--------------------");
        log.info("注册用户",JSON.toJSONString(usernameAndPassword));
        return  new JwtToken(ijwtService.registerUserAndGenerateToken(usernameAndPassword));
    }

}
